package com.lcxw.hxzbapi.tencentim.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.tencentim.FeignService.ImManager;
import com.lcxw.hxzbcommon.entity.TencentImConfigBean;
import com.tencentyun.TLSSigAPIv2;
import java.util.Random;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TencentImRealization {

    @Resource
    ImManager imManager;


    /**
     * 1、导入单个用户
     */
    public void importImUser(long userId) {
        this.importImUser(userId + "");
    }

    public void importImUser(String userId) {

        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
            TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Identifier", userId);
        String result = imManager
            .importAccount(TencentImConfigBean.APPID, admin, key, random, jsonObject);
        log.info("import user {} to tencent im " + result, userId);
    }

    /**
     * 2、修改用户的昵称/头像等
     */
    public void updateImUserInfo(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
            TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
            .updateUserInfo(TencentImConfigBean.APPID, admin, key, random, imBody);
//        log.info("update im user info " + result);
    }

    /*
     * 3、后台系统推送普通群消息
     */
    public void sendGroupMsg(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
            TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
            .sendGroupMsg(TencentImConfigBean.APPID, admin, key, random, imBody);
//        log.error("==imbody:" + imBody.toJSONString());
        log.info("发送群消息结果：" + result);
    }

    /**
     * 4、解散群组
     */
    public void destroyGroup(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
            TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
            .destroyGroup(TencentImConfigBean.APPID, admin, key, random, imBody);
        log.info("解散群组结果：" + result);
    }

    /**
     * 4、全员推送消息
     */
    public void sendAllMember(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
                TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
                .sendAllMember(TencentImConfigBean.APPID, admin, key, random, imBody);
        log.info("全员推送消息：" + result);
    }


    /**
     * 4、系统消息
     */
    public void sendmsg(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(TencentImConfigBean.APPID,
                TencentImConfigBean.KEY);
        long random = new Random().nextLong();
        String admin = TencentImConfigBean.ADMIN;
        String key = tlsSigAPIv2.genSig(admin, TencentImConfigBean.EXPIRE);
        String result = imManager
                .sendmsg(TencentImConfigBean.APPID, admin, key, random, imBody);
        log.info("全员推送消息：" + result);
    }


    /**
     * 构造imBody消息的方法
     */
    public JSONObject generateImBody(String msg, String roomId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("GroupId", roomId);
        imBody.put("Random", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");

        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "system");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);

        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);
        return imBody;

    }
    /**
     * 构造imBody消息的方法
     */
    public static JSONObject  generateImBody(String msg, String fromUserId,String toUserId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("From_Account", fromUserId);
        imBody.put("To_Account", toUserId);
        imBody.put("SyncOtherMachine",2);
        imBody.put("MsgRandom", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");

        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "message");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);

        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);

        return imBody;

    }
    /**
     * 构造系统消息的方法
     */
    public JSONObject generateUserBody(String msg, String userId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("To_Account", userId);
        imBody.put("SyncOtherMachine",2);
        imBody.put("MsgRandom", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");
        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "message");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);
        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);
        return imBody;
    }

    /**
     * 全员推送消息体
     */
    public JSONObject generateIm(String msg,String data) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");
        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", data);
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);
        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);
        return imBody;

    }

}
